const SERVICE = require("../../lib/js/service.js");

Page({
  data:{
    imgSrc: SERVICE.imgUrl,   //图片路径
    certification: ["未认证", "个体认证", "企业认证", "4S店认证", "审核中 ...", "审核不通过"],
    shareData: {
      show: false,
      quit: "shareQuit",
      share: "Share",
    },
    
  },
  onLoad(options){
    this.InitDetail(options.rental_id, options.demand_id);
    if (options){
      if (options.rental_id){
        wx.setNavigationBarTitle({
          title: "车位出租",
        });
      } else if (options.demand_id){
        wx.setNavigationBarTitle({
          title: "车位求租",
        });
      }
    }
  },

  /*分享*/
  onShareAppMessage() { 
    let td = this.data;
    return {
      title: td.shareData2.desc + ' | ' + td.shareData2.price + ' | ' + td.shareData2.rent_genre,
      imageUrl: td.imgSrc + td.shareData2.src
    }
  },

  /**
   * 加载初始数据
   * @param 1 出租
   * @param 2 求租
   * 
   * @Data detailInfo 详情
   * @Data scrollTop 回到顶部
   */ 
  InitDetail(param1, param2){
    let _this = this, td = this.data;

    wx.showNavigationBarLoading();
    SERVICE.POST(param1 ? SERVICE.URL.rentDetail : SERVICE.URL.MagdebrugDetail,{
      rental_id: param1,
      demand_id: param2
    },(status,res)=>{
      console.log("租值详情数据：",res);
      wx.hideNavigationBarLoading();
      if (status){
        let detailInfo = res.data.rental_list;
        let getDate = detailInfo.create_time.split("-");
        _this.setData({
          detailInfo: detailInfo,
          info: res.data,
          date: getDate[0] + "年" + getDate[1] + "月" + getDate[2] + "日",
          scrollTop: 0,
          id: res.data.rental_list.id 
        });
        _this.getRemmend({
          stauts: param1,
          Type: detailInfo.genre,
          province: detailInfo.province,
          city: detailInfo.city
        });
        _this.shareRentImage(detailInfo.id);
      }
    });
  },
  
  /*获取推荐*/ 
  getRemmend(obj){
    let _this = this;
    SERVICE.POST(obj.stauts ? SERVICE.URL.rentList : SERVICE.URL.Magdebrug, {
      page: 1,
      genre: obj.Type,
      province: obj.province,
      city: obj.city,
    }, (status, res) => {
      console.log("获取推荐:",res);
      if (status) {
        _this.setData({
          remment:res.data
        });
      }
    });
  },

  /*出租*/
  rent(e){
    this.InitDetail(e.currentTarget.dataset.id, "");
  },

  /*求租*/ 
  magdebrug(e){
    this.InitDetail("", e.currentTarget.dataset.id);
  },
  
  /*发布*/
  publish(){
    wx.navigateTo({
      url: "../publish/publish?pageStack=3"
    });
  },

  /*打电话*/ 
  ringUp(e) {
    let mobile = e.currentTarget.dataset.mobile;
    wx.showModal({
      content:  mobile,
      confirmColor: "#48BD59",
      success: function (res) {
        if (res.confirm) {
          wx.makePhoneCall({
            phoneNumber: mobile,
            success: function () {
              console.log("拨打电话成功！");
            },
            fail: function () {
              console.log("拨打电话失败！");
            }
          });
        }
      }
    })

  },

  /*分享按钮*/
  share() {
    this.data.shareData.show = true;
    this.setData({
      shareData: this.data.shareData
    });
  },

  /*分享退出按钮*/
  shareQuit() {
    console.log("yes");
    this.data.shareData.show = false;
    this.setData({
      shareData: this.data.shareData
    });
  },

  Share() {
    let td = this.data;
    wx.navigateTo({
      url: "../share-page/share-page?type=0&id=" + td.id,
    })
  },
  // 租赁
  shareRentImage(id) {
    let _this = this, td = this.data;
    SERVICE.POST(SERVICE.URL.shareRentImage, {
      rent_id: id,
      genre: 'flockPic'
    }, (status, res) => {
      _this.setData({
        shareData2: res.data
      });
    });
  },
});